<?php
//引入数据库类
require_once dirname(__FILE__).'/../dao/MysqlDb.class.php';
//require './com.lanxuewei.dao/MysqlDb.class.php';

/*
 * 管理员类(对managers表操作)
 */
class ManagersModel{
	//数据库
	private $mySqlDb;
	
	//测试
	private $TAG = "managersModel";
	
	//注册(首先根据account判断是否存在相同的数据,1.存在->返回false,表示注册失败,即账号名存在。2.否则->插入数据)
	public function register($account, $password, $sex){
		//echo "ManagersModel";
		//获取数据库实例
		$mySqlDb = MySqlDb::getMySqlDb();
		$mySqlDb->escapeString($account);
		$mySqlDb->escapeString($password);
		$mySqlDb->escapeString($sex);
		//sql
		$sql = "select `id` from managers where manager_account='$account';";
		$id = $mySqlDb->fetchRow($sql);
	
		//判断是否存在该用户名
		if(!empty($id)){
			return $errStr = "该账号已存在!";
		} else{
			//插入记录
			$sql = "insert into managers values(null, '$account', '$password', '$sex', 0);";
			$mySqlDb->escapeString($sql);
			$mySqlDb->query($sql);
			return true;
		}
	}
		
	
	//登录(使用账号查找匹配的密码,对比密码,1.密码正确->返回true。2.密码不正确->返回提示(账号不存在或者密码不正确)。3.密码正确->返回true)
	public function login($managerAccount, $managerPassword){
		//获取数据库实例
		$mySqlDb = MySqlDb::getMySqlDb();
		$mySqlDb->escapeString($managerAccount);
		$mySqlDb->escapeString($managerPassword);
		//echo "input:".$managerAccount."and".$managerPassword;
		//sql(取出密码和权限)
		$sql = "select `manager_password`,`right_control` from `managers` where manager_account ='$managerAccount';";
		//测试sql
		$managerInfo = $mySqlDb->fetchAll($sql);
		//var_dump($managerInfo);
		//echo $managerInfo[0]['manager_password'];
		//die("managerInfo = ".$managerInfo);
		//数据为空
		if(empty($managerInfo[0]['manager_password'])){
			//返回错误信息(账号不存在)
			//echo "*****";
			//die("失败");
			return $errStr = "账号不存在!";
		} else if($managerPassword !== $managerInfo[0]['manager_password']){
			//返回错误信息(密码不正确)
			return $errStr = "密码不正确!";
		} else {
			//密码相等,即登录账号正确
			//die("sucessful");
			return $managerInfo;
		}
	}
	
	//显示所有选课信息
	public function showAllCourses(){
		//获取数据库实例
		$mySqlDb = MySqlDb::getMySqlDb();
		$sql = "select * from courses";
		$showallCoursesInfo = $mySqlDb->fetchAll($sql);
		return $showallCoursesInfo;
	}
	
	//删除课程
	public function deleteCourse($courseId){
		//获取数据库实例
		$mySqlDb = MySqlDb::getMySqlDb();
		//转义数据
		$mySqlDb->escapeString($courseId);
		//先删除student_courses表中学生选课信息
		$sql = "delete from student_courses where `course_id`='$courseId';";
		$mySqlDb->query($sql);
		//删除courses表中的课程信息
		$sql = "delete from courses where `course_id`='$courseId';";
		$mySqlDb->query($sql);
	}
	
	//查询单项课程信息
	public function showSimpleCourse($couseId){
		//获得数据库实例
		$mySqlDb = MySqlDb::getMySqlDb();
		//转义数据
		$mySqlDb->escapeString($couseId);
		$sql = "select * from courses where `course_id`='$couseId'";
		$showSimpleCourseInfo = $mySqlDb->fetchRow($sql);
		return $showSimpleCourseInfo;
	}
	
	//修改选课信息
	public function reviseCourse($courseId, $courseName, $courseTeacher, $courseMaxCount){
		//获取数据库实例
		$mySqlDb = MySqlDb::getMySqlDb();
		//数据转义
		$mySqlDb->escapeString($courseId);
		$mySqlDb->escapeString($courseName);
		$mySqlDb->escapeString($courseTeacher);
		$mySqlDb->escapeString($courseMaxCount);
		
		//更新课程信息(如果参数为空则不执行修改)
		if($courseId==null&&$courseName==null&&$courseTeacher==null&&$courseMaxCount==null){
			return ;
		} else {
			//更新
			$sql = "update courses set `course_name`='$courseName',`course_student_count_max`='$courseMaxCount',`course_teacher_name`='$courseTeacher' where `course_id`='$courseId';";
			$mySqlDb->query($sql);
		}
	}
	
	//添加选修课程(传入课程编号、课程名、任课老师、最大人数)
	public function addCourse($courseId, $courseName, $courseTeacher, $courseMaxStudentCount){
		//获得数据库实例
		$mySqlDb = MySqlDb::getMySqlDb();
		//数据转义
		$mySqlDb->escapeString($courseId);
		$mySqlDb->escapeString($courseName);
		$mySqlDb->escapeString($courseTeacher);
		$mySqlDb->escapeString($courseMaxStudentCount);
		//先查询是否已经存在课程编号
		$sql = "select * from courses where course_id='$courseId';";
		$canInsert = $mySqlDb->fetchRow($sql);
		//非空表示已经存在该课程编号
		if(!empty($canInsert)){
			return false;
		} else {
			//插入课程信息
			$sql = "insert into courses values('$courseId', '$courseName', '$courseMaxStudentCount', 0, '$courseTeacher');";
			$mySqlDb->query($sql);
			return true;
		}
	}
	
}
//单元测试
//登录测试
//$test = new ManagersModel();

/*$managerAccount = 'lanxuewei';
$managerPassword = '520';
$login = $test->login($managerAccount, $managerPassword);
echo "登录信息为：账号："; 
print_r($login); */

//查看所有课程测试
/* $allCourses = $test->showAllCourses();
foreach($allCourses as $row){
	echo '课程编号：'.$row['course_id'].'&nbsp&nbsp';
	echo '课程名称：'.$row['course_name'].'&nbsp&nbsp';
	echo '任课老师'.$row['course_teacher_name'].'&nbsp&nbsp';
	echo '当前人数：'.$row['course_student_count_current'].'&nbsp&nbsp';
	echo '最大人数：'.$row['course_student_count_max'].'<br/>';
} */
?>



